'use strict';

/**
 * @ngdoc directive
 * @name webApp.directive:toTop
 * @description
 * # toTop
 */

angular.module('webApp')
  .directive('toolBox', function (jqLite,$ionicGesture,$ionicScrollDelegate,$location,popBox,$animate) {
  return {
    templateUrl: 'views/toolbox.html',
    restrict: 'AE',
    link: function postLink($scope, element, attrs) {
      //移动盒子
      $ionicGesture.on('drag',function(data){
        $scope.moveDoc = {
          'top': data.gesture.center.pageY - 20 + 'px',
          'left': data.gesture.center.pageX - 20 + 'px'
        };
        $scope.moveDocLeft = {
          'top': data.gesture.center.pageY - 20 + 'px',
          'left': data.gesture.center.pageX - 70 + 'px'
        };
        $scope.moveDocRight = {
          'top': data.gesture.center.pageY - 20 + 'px',
          'left': data.gesture.center.pageX + 30 + 'px'
        };
        $scope.moveDocTop = {
          'top': data.gesture.center.pageY - 70 + 'px',
          'left': data.gesture.center.pageX - 20 + 'px'
        };
        $scope.moveDocBottom = {
          'top': data.gesture.center.pageY + 30 + 'px',
          'left': data.gesture.center.pageX - 20 + 'px'
        };
        $scope.moveDocLeftTop = {
          'top': data.gesture.center.pageY - 70 + 'px',
          'left': data.gesture.center.pageX - 70 + 'px'
        };
        $scope.moveDocRightTop = {
          'top': data.gesture.center.pageY - 70 + 'px',
          'left': data.gesture.center.pageX + 30 + 'px'
        };
        $scope.moveDocLeftBottom = {
          'top': data.gesture.center.pageY + 30 + 'px',
          'left': data.gesture.center.pageX - 70 + 'px'
        };
        $scope.moveDocRightBottom = {
          'top': data.gesture.center.pageY + 30 + 'px',
          'left': data.gesture.center.pageX + 30 + 'px'
        };
        $scope.$apply();
      },element);
      //打开关闭盒子
      $scope.toolBoxDB = function(){
        if(!jqLite.$('toTop1').hasClass('toTop1Add')){
          $animate['addClass'](jqLite.$('toTop1'),'toTop1Add');
          $animate['addClass'](jqLite.$('toTop2'),'toTop2Add');
          $animate['addClass'](jqLite.$('toTop3'),'toTop3Add');
          $animate['addClass'](jqLite.$('toTop4'),'toTop4Add');
          $animate['addClass'](jqLite.$('toTop5'),'toTop5Add');
          $animate['addClass'](jqLite.$('toTop6'),'toTop6Add');
          $animate['addClass'](jqLite.$('toTop7'),'toTop7Add');
          $animate['addClass'](jqLite.$('toTop8'),'toTop8Add');
        }
        else{
          $animate['removeClass'](jqLite.$('toTop1'),'toTop1Add');
          $animate['removeClass'](jqLite.$('toTop2'),'toTop2Add');
          $animate['removeClass'](jqLite.$('toTop3'),'toTop3Add');
          $animate['removeClass'](jqLite.$('toTop4'),'toTop4Add');
          $animate['removeClass'](jqLite.$('toTop5'),'toTop5Add');
          $animate['removeClass'](jqLite.$('toTop6'),'toTop6Add');
          $animate['removeClass'](jqLite.$('toTop7'),'toTop7Add');
          $animate['removeClass'](jqLite.$('toTop8'),'toTop8Add');
        }
      };
      //历史回退
      $scope.goLeft = function(){
        history.go(-1);
      };
      //历史前进
      $scope.goRight = function(){
        history.go(+1);
      };
      //滚动条向上
      $scope.goUp = function(){
        $ionicScrollDelegate.scrollTop(true);
      };
      //滚动条向下
      $scope.goDown = function(){
        $ionicScrollDelegate.scrollBottom(true);
      };
      //阅读模式
      $scope.readBook = function(){
        if($location.$$path != '/root/mainText'){
          popBox.showConfirm('警告','请在新闻页使用阅读模式！');
        }
        else{
          $location.path('/root/reading');
        }
      };
      //天气预报
      $scope.weather = function(){
        popBox.showAlert();
      };
    }
  };
});

